﻿filter Format-Grid
{
    param ([string]$properties)

    begin
    {
        if (($properties -eq $false) -or ([string]::IsNullOrEmpty($properties)))
        {
            return $_
        }
        $obj = New-Object System.Object
        $allProperties = New-Object System.Collections.Hashtable
        $propertiesWanted = $properties.split(' ')
        $initialized = $false
    }
    process
    {
        $input = $_
        if ($initialized -eq $false)
        {           
            $objProperties = $input | Get-Member | where {$_.MemberType -like "*property"}
 
            foreach ($p in $propertiesWanted)
            {
                $exist = $input | Get-Member | where {($_.MemberType -like "*property") -and ($_.Name -eq "$p")}
                    
                if ($exist -eq $null)
                {
                    $exist = $input | Get-Member | where {($_.MemberType -like "*property") -and ($_.Name -like "*$p*") -and ($_.name.substring(0,1) -ne "_")}
                    if ($exist -ne $null)
                    {
                        foreach ($objP in $exist)
                        {
                            if ($allProperties.ContainsKey($objP.Name) -eq $false)
                            {
                                $list = New-Object System.Collections.ArrayList
                                    
                                Add-Member -i $obj -type "noteproperty" -name $objP.Name -value $list
                                $allProperties[$objP.Name] = $true
                            }
                        }
                    }
                }
                else
                {
                    $list = New-Object System.Collections.ArrayList
                                
                    Add-Member -i $obj -type "noteproperty" -name $exist.Name -value $list
                    $allProperties[$exist.Name] = $true
                }
            }
            $initialized = $true
        }
        foreach ($prop in $allProperties.Keys)
        {
            [void]$obj.$prop.Add($input.$prop)
        }
    }
    end
    {
        $allProperties = $null
        $propertiesWanted = $null
        return $obj
    }
}

